package cn.cnlee.app;

import android.content.ComponentCallbacks2;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tqzhang.stateview.core.LoadState;
import com.tqzhang.stateview.stateview.SuccessState;

import cn.cnlee.app.config.Urls;
import cn.cnlee.app.local.db.MyObjectBox;
import cn.cnlee.app.remote.interceptor.AutoLoginInterceptor;
import cn.cnlee.arch.base.BaseApplication;
import cn.cnlee.arch.http.CacheInterceptor;
import cn.cnlee.arch.http.RemoteClient;
import cn.cnlee.arch.view.stateview.ErrorState;
import cn.cnlee.arch.view.stateview.LoadingState;
import io.objectbox.BoxStore;

public class App extends BaseApplication implements ComponentCallbacks2 {

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.greenyellow, android.R.color.white);//全局设置主题颜色
                return new MaterialHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

    private BoxStore boxStore;//数据库管理者

    @Override
    public void onCreate() {
        super.onCreate();
        boxStore = MyObjectBox.builder().androidContext(this).build();
        new RemoteClient.Builder(this)
                .initOkHttp()
                .addInterceptor(new CacheInterceptor())
                .addInterceptor(new AutoLoginInterceptor())
                .createRetrofit(Urls.BASE_URL)
                .build();
        new LoadState.Builder()
                .register(new ErrorState())
                .register(new LoadingState())
                .setDefaultCallback(SuccessState.class)
                .build();
    }

    public BoxStore getDB() {
        return boxStore;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            Glide.get(this).clearMemory();
        }
        Glide.get(this).trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.get(this).clearMemory();
    }
}
